package com.wms.service.impl;

import com.wms.entity.Category;
import com.wms.mapper.CategoryMapper1;
import com.wms.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@EnableTransactionManagement
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper1 categoryMapper1;

    @Override
    public List<Category> getCategory() {
        return categoryMapper1.getCategory();
    }
    @Transactional
    public int saveCategory(Category category){
        String code=getNewCode(category.getParentId());
        category.setCode(code);
        return categoryMapper1.saveCategory(category);
    }


    //自动生成编码
    private String getNewCode(int pid){
        String newCode=null;
        String maxCode= categoryMapper1.queryMaxCode(pid);
        if (null==maxCode){
            //编码不存在
            if (pid==0){
                newCode="10";
            }else{
                String temCode= categoryMapper1.queryCode(pid);
                newCode=temCode+"01";
            }
        }else {
            //编码存在
            long tmp=Long.parseLong(maxCode);
            tmp++;
            newCode=String.valueOf(tmp);
        }
        return newCode;
    }
}
